unit uPacket;

interface
                                   
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ToolWin, ComCtrls, ScktComp;

function OleVariantToStringList(ov:OleVariant; slOut:TStrings):Boolean;
function StringListToOleVariant(sl:TStrings;var ov:OleVariant):Boolean;
function ParsePacket(sIn:String;lsHead, lsBody :TStrings;var sError:String):boolean;
function BuildPacket(Var sOut:String;lsHead, lsBody :TStrings;var sError:String):boolean;
function BuildPacket1(Var sOut:String;sCode,sFrom,sTo, sBody :String;var sError:String):boolean;

implementation
//the format
//#1<CODE>    --code is 9999
//#1<FromUser>
//#1<TOUser>#1
//#3#4
//#2<p1>
//#2<p2>
//...
//#2<pn>#2
//#4#3          -- the end of the package
function ParsePacket(sIn:String;lsHead, lsBody :TStrings;var sError:String):boolean;
var
  i,p1,p2:Integer;
  sHead,sBody:String;
begin
  Result:=False;
  sError:='';
  try
    //judge whether it is a valid package
    if (lsHead=nil) or (lsBody=nil) then exit;
    lsHead.Clear;
    lsBody.Clear;
    i:= Pos(#3#4 ,sIn);
    if i<>0 then
    begin
      sHead:=Copy(sIn,1,i-1);
      sBody:=Copy(sIn,i+2,Length(sIn)-i-1-2);
    end;
    p1:=0;
    p2:=0;
    for i:=1 to Length(sHead) do
    begin
      if sHead[i]=#1 then
      begin
        if p1=0 then
        begin
          p1:=i;
        end
        else
        begin
          p2:=i;
          lsHead.Add(COpy(sHead,p1+1,p2-p1-1));
          p1:=p2;
          p2:=0;
        end;
      end;
    end;

    p1:=0;
    p2:=0;
    for i:=1 to Length(sBody) do
    begin
      if sBody[i]=#2 then
      begin
        if p1=0 then
        begin
          p1:=i;
        end
        else
        begin
          p2:=i;
          lsBody.Add(COpy(sBody,p1+1,p2-p1-1));
          p1:=p2;
          p2:=0;
        end;
      end;
    end;
    Result:=True;
  except
    On E:Exception do
    begin
      sError:=E.Message;
    end;
  end;
end;

function BuildPacket(Var sOut:String;lsHead, lsBody :TStrings;var sError:String):boolean;
var
  i:Integer;
begin
  Result:=False;
  try
    sOut:='';

    for i:=0 to lsHead.Count-1 do
    begin
      sOut:=sOut+#1+lsHead[i];
    end;

    sOut:=sOut+#1;
    sOut:=sOut+#3#4;
    for i:=0 to lsBody.Count-1 do
    begin
      sOut:=sOut+#2+lsBody[i];
    end;
    sOut:=sOut+#2;
    sOut:=sOut+#4#3;
    Result:=True;
  except
    On E:Exception do
    begin
      sError:=E.Message;
    end;
  end;
end;


function BuildPacket1(Var sOut:String;sCode,sFrom,sTo,sBody :String;var sError:String):boolean;
var
  lsHead,lsBody:TStringList;
begin

  lsHead:=TStringList.Create;
  lsBody:=TStringList.Create;
  try
    lsHead.Add(sCode);
    lsHead.Add(sFrom);
    lsHead.Add(sTo);
    lsBody.Add(sBody);
    Result:=BuildPacket(sOut,lsHead,lsBody,sError);
  finally
    lsHead.Free;
    lsBody.Free;
  end;
end;

function StringListToOleVariant(sl:TStrings;var ov:OleVariant):Boolean;
var
  i:Integer;
begin
  Result:=False;
  try
    ov:=VarArrayCreate([0,sl.Count-1],VarVariant);
    for i:=0 to sl.Count-1 do
      ov[i]:=sl[i];
    Result:=True;
  except
  end;

end;

function OleVariantToStringList(ov:OleVariant; slOut:TStrings):Boolean;
var
  i,l,h:Integer;
begin
  Result:=False;
  try
    l:=VarArrayLowBound(ov,1);
    h:=VarArrayHighBound(ov,1);
    for i:=l to h do
    begin
      slOut.Add(ov[i]);
    end;
    Result:=True;
  except
  end;

end;

end.
